// subPages/health/bloodFat/bloodFat.js
var util =  require('../../../utils/util.js');
var navListJson = require('../../../data/navListJson');
var indexJson = require('../../../data/indexJson');
var recordJson = require('../../../data/recordJson');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setDate()
  },

  bindDateChange: function(e) {
    var that = this
    that.setData({
      date: e.detail.value,
    })
    this.getData(that.data.date)
  },

  //设置日期
  setDate:function(){
    var that = this
    let date = util.setTime10(new Date())
    let createtime = util.setTime(new Date())
    let Y = date.split('-')
    that.setData({
      createtime: createtime,
      date: date,
      dateStart: Y[0]- 3,
      dateEnd: date,
      navList: navListJson.bodyFatNavList,
      thList: navListJson.bodyFatThList
    })
    that.getData(date)
  },
  

  //获得数据
  getData:function(date){
    var that = this
    wx.request({
        url: app.globalData.apiUrl +'/health/lst.java',
        data: {
          userId: app.globalData.userId,
          type : 2,
          time: date
        },
        method: 'get',
        header: {
          'content-type': 'application/json' //默认值
        },
        success: function (res) {
          // console.log(res)
          if(res.data.code == '200'){
            that.processingEchartData(res.data.data);
            that.processingData(res.data.data);
          }else{
            util.showModal('提示','数据异常')
            return;
          }
        },
        fail: function (res) {
          console.log("失败",res);
        }
    })
  },

  //处理图表数据
  processingEchartData:function(list){
    var that = this
    if(list.length != 0){
      var fatList = indexJson.fatList
      for(let i in fatList)
      {
        switch (fatList[i].index) {
          case 1:
            fatList[i].data = list[0].cholesterol
            break;
          case 2:
            fatList[i].data = list[0].glycerol
            break;
          case 3:
            fatList[i].data = list[0].uric
            break;
          case 4:
            fatList[i].data = list[0].hdl
            break;
          case 5:
            fatList[i].data = list[0].ldl
            break;
        }
      }
      that.setData({
        isData: true,
        createtime: list[0].createtime.substring(0,10),
        dataList: fatList,
      })
    }else{
      that.setData({
        isData: false,
        dataList: indexJson.fatList2,
        tipTxt: {
          tipTxt: '暂无测量数据',
          tipImg: '/resources/index/noData.png'
        }
      })
    }
  },

  //处理数据
  processingData:function(list){
    var that = this
    if(list.length != 0){
      for(var i in list){
        var date = list[i].createtime.substring(8,10)
        list[i].createtime = date + '日'
      }
      that.setData({
        isData2: true,
        historyList: list,
      })
    }else{
      that.setData({
        isData2: false,
        tipTxt2: {
          tipTxt2: '暂无测量数据',
          tipImg2: '/resources/index/noData.png'
        }
      })
    }
  },

  //记录按钮
  recordBtn: function (e) {
    // console.log(e);
    var currentStatu = e.currentTarget.dataset.statu;
    this.anim(currentStatu);
  },

  //动画部分
  anim: function (currentStatu) {
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });
    
    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;
 
    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是400px)，停
    animation.translateY(400).step();
 
    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export()
    })
    
    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画：Y轴不偏移，停
      animation.translateY(0).step()
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        animationData: animation
      })
      
      //完成
      if (currentStatu == "complete") {
        this.setData(
          {
            isHeight: false,
          }
        );
      }

      //取消
      else if (currentStatu == "cancel") {
        this.setData(
          {
            isHeight: false,
          }
        );
      }
    }.bind(this), 200)
  
    // 显示抽屉
    if (currentStatu == "open") {
      this.setData(
        {
          isHeight: true,
          title: '记录血脂',
          recordList: recordJson.bloodFat,
        }
      );
    }
  },

  //提交表单
  formSubmit:function(e){
    // console.log(e)
    var that = this
    let cholesterol = e.detail.value.cholesterol;//总胆固醇
    let glycerol = e.detail.value.glycerol;//甘油三酯
    let uric = e.detail.value.uric;//尿酸
    let hdl = e.detail.value.hdl;//HDL
    let ldl = e.detail.value.ldl;//LDL
    if (cholesterol == "" || glycerol == "" || uric == "" || hdl == "" || ldl == ""){
      util.showModal('提示','请输入完整信息')
      return
    }
    if(cholesterol.length > 0)
    {
      let type= 'number'
      let validation = util.validation(cholesterol,type)
      if(!validation){
        util.showModal('提示','总胆固醇输入不正确')
        return
      }
    }
    if(glycerol.length > 0)
    {
      let type= 'number'
      let validation = util.validation(glycerol,type)
      if(!validation){
        util.showModal('提示','甘油三酯输入不正确')
        return
      }
    }
    if(uric.length > 0)
    {
      let type= 'number'
      let validation = util.validation(uric,type)
      if(!validation){
        util.showModal('提示','尿酸输入不正确')
        return
      }
    }
    if(hdl.length > 0)
    {
      let type= 'number'
      let validation = util.validation(hdl,type)
      if(!validation){
        util.showModal('提示','HDL输入不正确')
        return
      }
    }
    if(ldl.length > 0)
    {
      let type= 'number'
      let validation = util.validation(ldl,type)
      if(!validation){
        util.showModal('提示','LDL输入不正确')
        return
      }
    }
    that.save(cholesterol,glycerol,uric,hdl,ldl);
  },

  //保存信息
  save:function(cholesterol,glycerol,uric,hdl,ldl){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/health', //接口地址
      data: {
        userId: app.globalData.userId,
        type: 2,
        cholesterol: cholesterol,
        glycerol: glycerol,
        uric: uric,
        hdl: hdl,
        ldl: ldl,
      },
      method: 'post',
      header: {
          'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          that.getData(that.data.date);
        }else{
          util.showModal('提示','记录失败')
          return;
        }
      }
    })
  },

  //选择导航栏
  switchNav(event){
    var that = this
    var cur = event.currentTarget.dataset.current;      
    if (that.data.currentTab == cur) {
      return false;
    } else {
      that.setData({
          currentTab: cur
      })
    }
  },

  //阻止穿透
  preventTouchMove: function () {
    // console.log('stop user scroll it!');
    return;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})